<template>
  <div class="search-container">
    <!-- 搜索组件一级路由   $router.back()返回上一个页面-->
    <van-nav-bar
      left-arrow
      title="搜索中心"
      @click-left="$router.push('/home')"
    >
    </van-nav-bar>

    <!-- https://youzan.github.io/vant/#/zh-CN/search -->
    <van-search
      placeholder="请输入搜索关键词"
      shape="round"
      v-model.trim="keyword"
      @input="keywordchange"
      @search="toresult"
    >
      <template #action>
        <div>搜索</div>
      </template>
    </van-search>

    <!-- 联想建议 -->
    <van-cell-group>
      <van-cell
        v-for="(item, index) in highlightkeyword"
        :key="index"
        icon="search"
        @click="gotoresult(index)"
        ><template #title>
          <div v-html="item"></div>
        </template>
      </van-cell>
    </van-cell-group>
    <!-- /联想建议 -->
  </div>
</template>

<script>
import { getSuggestion } from '../../api/search'
export default {
  name: 'search',
  data () {
    return {
      keyword: '', // 搜索关键字,
      suggestionarr: [],
      timeoutid: undefined,
      cansearch: true
    }
  },
  computed: {
    highlightkeyword () {
      const opts = []
      this.suggestionarr.forEach(item => {
        const newvalue = item.replace(
          this.keyword,
          `<span style = 'color:red'>${this.keyword}</span>`
        )
        opts.push(newvalue)
      })
      return opts
    }
  },
  methods: {
    // 去result页面
    toresult () {
      if (this.keyword !== '') {
        this.$router.push({
          path: '/result',
          query: { searchvalue: this.keyword }
        })
      }
    },
    gotoresult (index) {
      this.$router.push({
        path: '/result',
        query: { searchvalue: this.suggestionarr[index] }
      })
    },
    // async keywordchange () {
    //   if (this.keyword === '') {
    //     this.suggestionarr = []
    //     return
    //   }
    //   const res = await getSuggestion({ q: this.keyword })
    //   console.log(res)
    //   this.suggestionarr = res.data.data.options
    // }

    // 防抖优化 性能优化
    // keywordchange () {
    //   if (this.keyword === '') {
    //     this.suggestionarr = []
    //     return
    //   }
    //   clearTimeout(this.timeoutid)
    //   this.timeoutid = setTimeout(async () => {
    //     if (this.keyword === '') {
    //       clearTimeout(this.timeoutid)
    //       return
    //     }
    //     const res = await getSuggestion({ q: this.keyword })
    //     console.log(res)
    //     this.suggestionarr = res.data.data.options
    //   }, 500)
    // }

    // 节流优化
    async keywordchange () {
      if (this.keyword === '') {
        this.suggestionarr = []
        return
      }
      if (this.cansearch === true) {
        this.cansearch = false
        const res = await getSuggestion({ q: this.keyword })
        console.log(res)
        this.suggestionarr = res.data.data.options
        setTimeout(() => {
          this.cansearch = true
        }, 5000)
      }
    }
  }
}
</script>

<style lang="less">
.search-container {
  .van-nav-bar {
    background-color: #3296fa;
    .van-nav-bar__title {
      color: white;
    }
    .van-icon {
      color: white;
    }
  }
}
</style>
